<?php

namespace App\Http\Controllers;

use DB;
use Cache;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

use EasyWeChat\Foundation\Application;

use App\Http\Models\Captcha;
use App\Http\Models\ObjectModel;
use App\Http\Models\Order;
use App\Http\Models\Price;
use App\Http\Models\Line;
use App\Http\Models\User;
use App\Http\Models\Record;
use App\Http\Models\Remind;
use App\Http\Models\PayRequest;

class ApiController extends Controller
{

    protected function insertLineItem($period, $object, $objectPrice, $objectPriceTime)
    {
        if (Line::where('id_object', $object->id)->where('body_period', $period)->where('created_at', '>=', date('Y-m-d H:i:s', time() - $period))->count() == 0) {
            $line = new Line;
            $line->id_object = $object->id;
            $line->body_period = $period;
            $line->body_open = sprintf('%.' . $object->body_price_decimal . 'f', $objectPrice);
            $line->body_high = $line->body_open;
            $line->body_low = $line->body_open;
            $line->body_close = $line->body_open;
            $line->body_volume = 0;
            $line->save();
            Line::where('id_object', $object->id)->where('body_period', $period)->where('created_at', '<', date('Y-m-d H:i:s', strtotime($objectPriceTime) - $period * 600))->delete();
        } else {
            $line = Line::where('id_object', $object->id)->where('body_period', $period)->where('created_at', '>=', date('Y-m-d H:i:s', time() - $period))->first();
            $line_closed_at = strtotime($line->created_at) + $line->body_period;
            if (time() < $line_closed_at) {

                $rel = array();
                if ($objectPrice > $line->body_high) {
                    $rel['body_high'] = sprintf('%.' . $object->body_price_decimal . 'f', $objectPrice);
                }
                if ($objectPrice < $line->body_low) {
                    $rel['body_low'] = sprintf('%.' . $object->body_price_decimal . 'f', $objectPrice);
                }
                $rel['body_close'] = sprintf('%.' . $object->body_price_decimal . 'f', $objectPrice);
                $rel['body_volume'] = $line->body_volume + mt_rand(0, 1);
                $rel['created_at'] = $line->created_at;
                $rel['updated_at'] = date('Y-m-d H:i:s', time());
                Line::whereId($line->id)->update($rel);
            }
        }
    }

    protected function insertLine($object, $objectPrice, $objectPriceTime)
    {
        if ($object->body_status == 1) {
            $this->insertLineItem(60, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(300, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(900, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(1800, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(3600, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(86400, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(604800, $object, $objectPrice, $objectPriceTime);
            $this->insertLineItem(2592000, $object, $objectPrice, $objectPriceTime);
        }

    }

    protected function insertPrice($object, $objectPrice, $objectPriceTime, $objectPriceMin, $objectPriceMax, $objectPriceInterval)
    {
        if (Price::where('id_object', $object->id)->where('body_price_time', date('Y-m-d H:i:s', time()))->count() == 0) {
            if (Price::where('id_object', $object->id)->where('body_price_time', $objectPriceTime)->count() == 0) {

                if (floatval($object->body_price) == floatval($objectPrice)) {
                    $object->body_price_repeat = intval($object->body_price_repeat) + 1;
                    if ($object->body_price_repeat >= 180) {
                        $object->body_status = 0;
                    }
                } else {

                    $object->body_price_repeat = 0;
                    $object->body_status = 1;

                    $price = new Price;
                    $price->id_object = $object->id;
                    $price->body_price = sprintf('%.' . $object->body_price_decimal . 'f', $objectPrice);
                    $price->body_price_time = $objectPriceTime;
                    $price->save();

                    $object->body_price_previous = $object->body_price;
                    $object->body_price = $price->body_price;
                    $object->body_price_min = $objectPriceMin;
                    $object->body_price_max = $objectPriceMax;
                    $object->body_price_interval = $objectPriceInterval;
                    $object->save();

                    Price::where('id_object', $object->id)->where('body_price_time', '<', date('Y-m-d H:i:s', strtotime($price->body_price_time) - 12800))->delete();

                }


            }
        } else {
            $price = Price::where('id_object', $object->id)->where('body_price_time', date('Y-m-d H:i:s', time()))->first();
            $object->body_price_previous = $object->body_price;
            $object->body_price = $price->body_price;
            $object->save();
        }
        $this->insertLine($object, $objectPrice, $objectPriceTime);

    }

    public function fetch()
    {

        //$requestResults = file_get_contents('http://m.svsfx688.com/json_objects.php?_=' . mt_rand(100000000, 999999999));
        $requestResults = file_get_contents('http://forex.wiapi.hexun.com/forex/sortlist?block=303&number=1000&title=15&commodityid=0&direction=0&start=0&column=code,name,type,price');
        header("Content-type: text/html; charset=utf-8");
        $requestResults = trim($requestResults, '();');
        //比特币
        $sendUrl = 'https://api.walian.cn/coins/chainIndex.open?en_name=BTC'; //短信接口的URL
        $smsConf = '';
        $content = $this->juhecurl($sendUrl,$smsConf,1);
        $a = json_decode($content,true);
        //获取上一个值。如果一样就随机，如不一样就显示真实值
        $up = ObjectModel::where('body_tag_forex','USDCHF')->first();
        $btc = $a['data']['coinIndex']['nowPrice'];
        if($btc == $up->body_price){
            $btc = (($up->body_price * 10000) + rand(10000,50000)) / 10000;
        }
        $requestResults = json_decode($requestResults, TRUE);
        $objects = ObjectModel::where('id', '<', 18)->get();
        $requestResults = $requestResults['Data'][0];
        $arr = array();
        foreach ($requestResults as $k => $v) {
            if($v[0] == 'USDCHF'){//比特币
                $arr[$v[0]] = $btc * 10000;
            }else{
                $arr[$v[0]] = $v[3];
            }
        }
        $i = 0;
        foreach ($objects as $item) {
            $this->insertPrice($item, ($arr[$item->body_tag_forex] / 10000), date('Y-m-d H:i:s', time()), ($arr[$item->body_tag_forex] / 10000), ($arr[$item->body_tag_forex] / 1000), 0);
            $i++;
        }
        echo('Fetch Done!');
    }
    public function btc(){
        $sendUrl = 'https://api.walian.cn/coins/getHuobiHistoryNew.open?symbol=btcusdt&period=30min&size=1'; //短信接口的URL
        $smsConf = '';
        $content = $this->juhecurl($sendUrl,$smsConf,1);
    }
    public function juhecurl($url,$params=false,$ispost=0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    public function btfetch()
    {
        $requestString = 'http://api.huobi.com/staticmarket/ticker_btc_json.js';
        $requestResults = explode(',', file_get_contents($requestString));

        $btjia = explode(':', $requestResults[7]);
        $btjia1 = $btjia[1];
        $btsjs = mt_rand(1, 9);
        $btjia1 = $btjia1 + $btsjs;
        $objects = ObjectModel::where('id', '=', 9)->get();
        foreach ($objects as $object) {
            $this->insertPrice($object, $btjia1, date('Y-m-d H:i:s', time()), $btjia1, $btjia1, 0);
        }
        echo('BTfetch Done!');
        echo $btjia1;
    }


    public function compute()
    {

        $orders = Order::where('striked_at', '0000-00-00 00:00:00')->get();
        foreach ($orders as $order) {

            $order_striked_time = strtotime($order->created_at) + $order->body_time;
            if ($order_striked_time <= time()) {

                if ($order->body_is_controlled == 0) {
                    $object_latestPrice = Price::where('id_object', $order->id_object)->where('body_price_time', '<=', date('Y-m-d H:i:s', $order_striked_time))->orderBy('created_at', 'desc')->first();
                    $order->body_price_striked = $object_latestPrice->body_price;
                }
                $order->striked_at = date('Y-m-d H:i:s', time());
                if (floatval($order->body_price_buying) == floatval($order->body_price_striked)) {
                    $order->body_is_win = 0;
                    $order->body_is_draw = 1;
                } else if (floatval($order->body_price_buying) < floatval($order->body_price_striked)) {
                    $order->body_is_win = $order->body_direction == 1 ? 1 : 0;
                    $order->body_is_draw = 0;
                } else {
                    $order->body_is_win = $order->body_direction == 0 ? 1 : 0;
                    $order->body_is_draw = 0;
                }
                $order->save();
                $user = User::where('id_wechat', $order->id_user)->first();
                if ($order->body_is_win == 1) {
                    $user->body_balance = $user->body_balance + $order->body_stake + $order->body_bonus;
                    $user->body_bonus = $user->body_bonus + $order->body_bonus;
                    $user->save();
                    $record = new Record;
                    $record->id_user = $user->id_wechat;
                    $record->id_order = $order->id;
                    $record->body_name = $order->body_direction == 1 ? '看涨盈利' : '看跌盈利';
                    $record->body_direction = 1;
                    $record->body_stake = $order->body_stake + $order->body_bonus;
                    $record->save();
                } else if ($order->body_is_draw == 1) {
                    $user->body_balance = $user->body_balance + $order->body_stake;
                    $user->save();
                    $record = new Record;
                    $record->id_user = $user->id_wechat;
                    $record->id_order = $order->id;
                    $record->body_name = $order->body_direction == 1 ? '看涨退回' : '看跌退回';
                    $record->body_direction = 1;
                    $record->body_stake = $order->body_stake;
                    $record->save();
                }
            }
        }

        echo('COMPUTE_DONE');


    }


    protected function computeNumber($number, $direction, $margin)
    {

        $numberExplode = explode('.', $number);
        $numberDecimal = end($numberExplode);
        $numberDecimalLength = strlen($numberDecimal);
        $numberDecimalControl = pow(0.1, $numberDecimalLength);

        if (env('ORDER_WILL_WIN')) {
            if ($direction == 1) {
                return floatval($number) + $margin * $numberDecimalControl;
            } else {
                return floatval($number) - $margin * $numberDecimalControl;
            }
        } else {
            if ($direction == 1) {
                return floatval($number) - $margin * $numberDecimalControl;
            } else {
                return floatval($number) + $margin * $numberDecimalControl;
            }
        }

    }

    protected function computeRate($id)
    {
        $orders_count = Order::where('id_user', $id)->count();
        if ($orders_count > 0) {
            $orders_is_win = Order::where('id_user', $id)->where('body_is_win', '1')->count();
            return ($orders_is_win / $orders_count) * 100;
        } else return 0;
    }

    protected function computePriceItem($time, $order, $object, $margin)
    {

        $price = Price::firstOrNew(array(
            'id_object' => $order->id_object,
            'body_price_time' => date('Y-m-d H:i:s', $time),
            'created_at' => date('Y-m-d H:i:s', $time)
        ));

        if ($order->body_stake > $price->body_rank) {
            $price->body_price = $this->computeNumber(sprintf('%.' . $object->body_price_decimal . 'f', $order->body_price_buying), $order->body_direction, $margin);
            $price->body_rank = $order->body_stake;
            $price->save();
        }
        return $price;

    }

    protected function computePrice($user, $order, $object)
    {

        $rate = $this->computeRate($user->id_wechat);

        if (floatval($order->body_stake) < 100) {
            $rate = 0;
        } else {
            if (floatval($order->body_stake) >= 200) $rate = $rate + 10;
            if (floatval($order->body_stake) >= 500) $rate = $rate + 10;
            if (floatval($order->body_stake) >= 1000) $rate = $rate + 15;
        }

        if (env('ORDER_WILL_LOST') ==1 || env('ORDER_WILL_WIN') == 1) {
            $rate = 101;
        }

        if (rand(1, 100) <= $rate) {

            $order_striked_time = strtotime($order->created_at) + $order->body_time;//到的时间

            $price = $this->computePriceItem($order_striked_time, $order, $object, mt_rand(1, 3));
//            $this->computePriceItem($order_striked_time - 9, $order, $object, 9);
//            $this->computePriceItem($order_striked_time - 8, $order, $object, 8);
//            $this->computePriceItem($order_striked_time - 7, $order, $object, 7);
//            $this->computePriceItem($order_striked_time - 6, $order, $object, 6);
//            $this->computePriceItem($order_striked_time - 5, $order, $object, 5);
//            $this->computePriceItem($order_striked_time - 4, $order, $object, 4);
//            $this->computePriceItem($order_striked_time - 3, $order, $object, 3);
//            $this->computePriceItem($order_striked_time - 2, $order, $object, 2);
//            $this->computePriceItem($order_striked_time - 1, $order, $object, 1);
//            $this->computePriceItem($order_striked_time + 1, $order, $object, 1);
//            $this->computePriceItem($order_striked_time + 2, $order, $object, -1);

            $order->body_price_striked = $price->body_price;
            $order->body_is_controlled = 1;
            $order->save();
        }

    }

    protected function computeNetwork($user, $order)
    {

        $introducerIndex = 0;   //推荐人等级
        while ($user->id_introducer != 0) {   //如果用户推荐人不为空
            $introducerIndex++;    //推荐人等级+1
            $introducer = User::where('id_wechat', $user->id_introducer)->first();  //从用户表查询有无此推荐人
            $introducer->body_transactions_network = $introducer->body_transactions_network + $order->body_stake; //推荐人的这个字段加上  此订单的金额

            if ($introducerIndex <= 3) {  //如果推荐人等级在三级以内

                $bonus = floatval($order->body_stake * floatval(env("AGENT_$introducerIndex"))); //定义奖金   等于订单金额*当前代理等级的金额。
                if ($bonus < 0.01) $bonus = 0.01;  //如果奖金小于0.01就默认为0.01

                $introducer->body_balance = floatval($introducer->body_balance) + $bonus;  //介绍人的余额加上奖金

                $record = new Record;   //新建存储记录
                $record->id_user = $introducer->id_wechat;  //用户为介绍人
                $record->id_refer = $user->id_wechat;  //
                $record->body_name = '推广收入';
                $record->body_direction = 1;
                $record->body_stake = $bonus;
                $record->save();

            }

            $introducer->save();
            return $introducer;
        }

    }

    public function automate()
    {

        $this->fetch();
        $this->btfetch();
        $this->compute();

        return response()->json([
            'result' => 'OK'
        ]);

    }

    public function objects()
    {
        $result = array(
            'timestamp' => time(),
            'objects' => array()
        );
        $objects = ObjectModel::where('is_disabled', '0')->orderBy('body_rank', 'desc')->get();
        $result['objects'] = $objects->toArray();
        if ($user = session('tel')) {
            $user = User::where('id_wechat', $user)->first();
            $result['user'] = $user->toArray();
        }
        return response()->json($result);
    }

    public function objectsDetail(Request $request, Response $response, $id, $period)
    {

        $object = ObjectModel::find($id);
        $lines = Line::where('id_object', $object->id)->where('body_period', $period)->orderBy('id', 'desc')->get();

        if ($object->body_status == 1) {
            $object['status'] = TRUE;
        } else {
            $object['status'] = FALSE;
        }

        $result = array(
            'timestamp' => time(),
            'period' => $period,
            'object' => $object,
            'lines' => $lines->toArray()
        );
        return response()->json($result);

    }

    public function objectsDetailUpdate(Request $request, Response $response, $id, $period)
    {

        $object = ObjectModel::find($id);
        $lines = Line::where('id_object', $object->id)->where('body_period', $period)->orderBy('id', 'desc')->first();

        if ($object->body_status == 1) {
            $object['status'] = TRUE;
        } else {
            $object['status'] = FALSE;
        }

        $result = array(
            'timestamp' => time(),
            'period' => $period,
            'object' => $object,
            'lines' => $lines->toArray()
        );

        return response()->json($result);

    }

    public function ordersDetail(Request $request, Response $response, $id)
    {

        $item = Order::find($id);
        $result = array(
            'timestamp' => time(),
            'item' => $item
        );

        return response()->json($result);

    }

    public function update(Request $request, Response $response)
    {

        $result = array(
            'user' => NULL,
            'timestamp' => time(),
            'date' => date('Y-m-d'),
            'time' => date('H:i:s'),
            'objects' => array()
        );

        if ($request->input('object', null)) {
            $objects = ObjectModel::where('id', $request->input('object'))->get();
        } else {
            $objects = ObjectModel::orderBy('body_rank', 'desc')->get();
        }

        foreach ($objects as $object) {
            $result_item = $object->toArray();
            $result_item_latestPrice = Price::where('id_object', $object->id)->orderBy('created_at', 'desc')->first();

            if ($request->input('mode') == 'fs') {
                $cacheTime = time();
                if (!Cache::has('prices_' . $object->id . '_' . $cacheTime)) {
                    Cache::put('prices_' . $object->id . '_' . $cacheTime, Price::where('id_object', $object->id)->where('body_price_time', '<=', date('Y-m-d H:i:s', $cacheTime))->where('body_price_time', '>', date('Y-m-d H:i:s', strtotime($result_item_latestPrice->body_price_time) - 900))->orderBy('created_at', 'desc')->get()->toArray(), 1);
                }
                $result_item['prices'] = Cache::get('prices_' . $object->id . '_' . $cacheTime);
            } else {

                if ($object->body_tag == 'sh000010' || $object->body_tag == 'sh000300') {
                    $requestString = 'http://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/DATA/CN_MarketData.getKLineData?symbol=#CODE#&scale=5&ma=no&datalen=30';
                } else if ($object->body_tag == 'gb_$ndx') {
                    $requestString = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/DATA/US_MinKService.getMinK?symbol=.ndx&type=5';
                } else if ($object->body_tag == 'gb_$dji') {
                    $requestString = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/DATA/US_MinKService.getMinK?symbol=.dji&type=5';
                } else if ($object->body_tag == 'gb_bidu') {
                    $requestString = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/DATA/US_MinKService.getMinK?symbol=bidu&type=5';
                } else if ($object->body_tag == 'gb_baba') {
                    $requestString = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/DATA/US_MinKService.getMinK?symbol=baba&type=5';
                } else if ($object->body_tag == 'gb_jd') {
                    $requestString = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/DATA/US_MinKService.getMinK?symbol=jd&type=5';
                } else {
                    $requestString = 'http://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/DATA/NewForexService.getMinKline?symbol=#CODE#&scale=5&datalen=30';
                }

                $requestString = str_replace('#CODE#', $object->body_tag, $requestString);
                $requestResult = explode('DATA(', file_get_contents($requestString));
                $requestResult = str_replace(')', '', $requestResult[1]);
                $requestResult = str_replace('{day:', '{d:', $requestResult);
                $requestResult = str_replace(',open:', ',o:', $requestResult);
                $requestResult = str_replace(',high:', ',h:', $requestResult);
                $requestResult = str_replace(',low:', ',l:', $requestResult);
                $requestResult = str_replace(',close:', ',c:', $requestResult);
                $requestResult = str_replace(',volume:', ',v:', $requestResult);
                $requestResult = str_replace('d', '"d"', $requestResult);
                $requestResult = str_replace('o', '"o"', $requestResult);
                $requestResult = str_replace('l', '"l"', $requestResult);
                $requestResult = str_replace('h', '"h"', $requestResult);
                $requestResult = str_replace('c', '"c"', $requestResult);
                $requestResult = str_replace('v', '"v"', $requestResult);
                $requestResult = str_replace(';', '', $requestResult);
                $result_item['prices'] = json_decode($requestResult, TRUE);
            }

            if ($object->body_status == 1) {
                $result_item['status'] = TRUE;
            } else {
                $result_item['status'] = FALSE;
            }

            if ($user = session('tel')) {
                $user = User::where('id_wechat', $user)->first();
                $result_item['orders'] = Order::where('id_object', $object->id)->where('id_user', $user->id_wechat)->where('striked_at', '0000-00-00 00:00:00')->orderBy('created_at', 'desc')->get()->toArray();
            }

            $result['objects'][] = $result_item;
        }

        if ($user = session('tel')) {

            $user = User::where('id_wechat', $user)->first();

            $latestStrikedOrder = Order::where('id_object', $object->id)->where('id_user', $user->id_wechat)->where('striked_at', '<>', '0000-00-00 00:00:00')->orderBy('created_at', 'desc')->first();
            if ($latestStrikedOrder) $latestStrikedOrder = $latestStrikedOrder->toArray();
            else $latestStrikedOrder = NULL;

            $result['user'] = array(
                'balance' => floatval($user->body_balance),
                'latestStrikedOrder' => $latestStrikedOrder
            );

        }

        return response()->json($result);

    }

    public function captchaCreate(Request $request, Response $response)
    {

        $code = mt_rand(100000, 999999);

        $captcha = new Captcha;
        $captcha->body_mobile = $request->input('mobile');
        $captcha->body_code = $code;
        $captcha->save();

        $requestUri = env('SMS_BASE');
        $requestUri = str_replace('#1#', env('SMS_KEY'), $requestUri);
        $requestUri = str_replace('#2#', $request->input('mobile'), $requestUri);
        $requestUri = str_replace('#3#', urlencode("【恒信】你的验证码是" . $code . "，请在10分钟内输入。"), $requestUri);

        $result = file_get_contents($requestUri);
        return response()->json(['result' => $result]);

    }

    public function orderCreate(Request $request, Response $response)
    {
        if (!session('tel')) {
            return response()->json([
                'error' => '身份验证失败，请重新打开页面再试'
            ]);
        }

        $carbon = Carbon::now();
        if ($carbon->hour < 6 && $carbon->hour > 2) {
            return response()->json([
                'error' => '非交易时间无法进行交易'
            ]);
        }
        if (is_null($request->input('object', NULL))
            || is_null($request->input('stake', NULL))
            || is_null($request->input('time', NULL))
            || is_null($request->input('direction', NULL))
        ) {
            return response()->json([
                'error' => '参数提交不全，请重新打开页面再试'
            ]);
        }

        if ($request->input('stake') < 9
            || $request->input('stake') > 99999
        ) {
            return response()->json([
                'error' => '交易量输入错误，10-99999之间'
            ]);
        }

        if ($request->input('time') != 60
            && $request->input('time') != 120
            && $request->input('time') != 180
            && $request->input('time') != 240
            && $request->input('time') != 300
            && $request->input('time') != 900
            && $request->input('time') != 1800
            && $request->input('time') != 3600
        ) {
            return response()->json([
                'error' => '参数提交错误，请重新打开页面再试'
            ]);
        }

        if ($request->input('direction') != 1
            && $request->input('direction') != 0
        ) {
            return response()->json([
                'error' => '参数提交错误，请重新打开页面再试'
            ]);
        }

        if (!$object = ObjectModel::find($request->input('object'))) {
            return response()->json([
                'error' => '参数提交错误，请重新打开页面再试'
            ]);
        }

        if ($object->is_disabled > 0) {
            return response()->json([
                'error' => '参数提交错误，请重新打开页面再试'
            ]);
        }

        if ((strtotime($object->updated_at) + 180) < time()) {
            return response()->json([
                'error' => '休市期间无法进行交易'
            ]);
        }

        if ($object->body_status == 0) {
            return response()->json([
                'error' => '休市期间无法进行交易'
            ]);
        }

        if (!$user = User::where('id_wechat', session('tel'))->first()) {
            return response()->json([
                'error' => '身份验证失败，请重新打开页面再试'
            ]);
        }

        if (floatval($user->body_balance) < $request->input('stake')) {
            return response()->json([
                'error' => '账户可用余额不足，请先充值后再交易'
            ]);
        }

        if ($user->is_disabled > 0) {
            return response()->json([
                'error' => '账户已被封禁，无法进行交易'
            ]);
        }

        DB::beginTransaction();

        $user->body_balance = floatval($user->body_balance) - $request->input('stake');
        $user->body_transactions = floatval($user->body_transactions) + $request->input('stake');
        $user->save();

        if ($user->body_balance < 0) {
            DB::rollback();
        } else {
            $profit = 1;
            if($request->input('time') == 60){
                $profit = 0.05;
            }else if($request->input('time') == 300){
                $profit = 0.1;
            }else if($request->input('time') == 900){
                $profit = 0.2;
            }else if($request->input('time') == 1800){
                $profit = 0.8;
            }else if($request->input('time') == 3600){
                $profit = 0.8;
            }
            $order = new Order;
            $order->id_user = $user->id_wechat;
            $order->id_object = $object->id;
            $order->body_price_buying = $object->body_price;
            $order->body_stake = $request->input('stake');
            $order->body_bonus = $profit * $request->input('stake');
            $order->body_direction = $request->input('direction');
            $order->body_time = $request->input('time');
            $order->save();

            $record = new Record;
            $record->id_user = $user->id_wechat;
            $record->id_order = $order->id;
            $record->body_name = $request->input('direction') == 1 ? '买入看涨' : '买入看跌';
            $record->body_direction = 0;
            $record->body_stake = $order->body_stake;
            $record->save();

            $remind = new Remind;
            $remind->type = 3;
            $remind->tel = $user->id_wechat;
            $remind->add_time = time();
            $remind->save();
        }

        DB::commit();

        if (!$order) {
            return response()->json([
                'error' => '下单过于频繁，请稍后再试'
            ]);
        } else {
            $this->computeNetwork($user, $order);

            if (env('ORDER_CONTROL')) {
                $this->computePrice($user, $order, $object);
            }

        }

        $result = $order->toArray();

        $order_striked_time = strtotime($order->created_at) + $order->body_time;
        $result['distance'] = intval($order->body_time);
        $result['distance_year'] = date('Y', $order_striked_time);
        $result['distance_month'] = date('m', $order_striked_time);
        $result['distance_day'] = date('d', $order_striked_time);
        $result['distance_hour'] = date('H', $order_striked_time);
        $result['distance_minute'] = date('i', $order_striked_time);
        $result['distance_second'] = date('s', $order_striked_time) + 1;

        return response()->json([
            'result' => $result
        ]);


    }

    public function payRequestUpdate(Request $request, Response $response, $id)
    {

        if (!$payRequest = PayRequest::find($id)) {
            return response()->json([
                'result' => 'FAIL'
            ]);
        }

        if ($payRequest->processed_at == '0000-00-00 00:00:00') {
            return response()->json([
                'result' => 'FAIL'
            ]);
        }

        return response()->json([
            'result' => 'OK'
        ]);

    }

}